package compiler;

import compiler.node.Program;
import org.antlr.runtime.RecognitionException;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class Compiler {
    public static void main(String[] args)
        throws IOException, RecognitionException{
        Compiler compiler = new Compiler();

        Program program = compiler.parse("((b bicondicional c)condicional(a bicondicional b))", true);
        System.out.println(program);
        program.analyze();
        boolean[] result = (boolean[])program.execute();
        for(int j=0;j<result.length;j++){
            System.out.println(""+result[j]);
        }
    }

    protected Program parse(String code, boolean abortOnError)
        throws IOException, RecognitionException{
        return parse(new StringReader(code), abortOnError);
    }

    protected Program parse(Reader reader, boolean abortOnError)
        throws IOException, RecognitionException {
        MyParser parser = new MyParser(reader);
        MyParser.program_return program = parser.program();

        if (abortOnError && parser.getError() != null) {
            throw parser.getError();
        }

        return (Program) program.getTree();
    }
}
